/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Excersize2_Swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import Excersize2_Swing.BattleshipGameJFrame;
import battleshipgame.logic.Battleship;
import battleshipgame.logic.Board.SquareStatus;
import battleshipgame.logic.Player.Direction;
import battleshipgame.logic.Square;

/**
 *
 * @author אלירן
 */
public class SquareButton extends JButton {

    Square squareRefference;
    String coordinate;
    BattleshipGameJFrame referBattleshipGameJFrame;

    public SquareButton(String coordinate, Square square, BattleshipGameJFrame bgjf) {
        this.coordinate = coordinate;
        this.referBattleshipGameJFrame = bgjf;
        squareRefference = square;
        //JLabel lable = new JLabel(); //TODO : maybe picture in lable.
        this.setBackground(Color.blue);
        SquareStatus status = squareRefference.getStatus();
        if (status == SquareStatus.BATTLESHIP) {
            this.setBackground(Color.red);
        }
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        //this.add( lable );
        this.setMinimumSize(new Dimension(20, 20));
        this.setMaximumSize(new Dimension(20, 20));
        this.setPreferredSize(new Dimension(20, 20));
        this.setVisible(true);
        addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                
            }

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SquareButtonMouseClicked(evt);
            }
        });
    }

    public String getCoordinate() {
        return coordinate;
    }

    private void SquareButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
        if (!referBattleshipGameJFrame.currentCommand.IsEnabled()) {
            if ( squareRefference.getStatus() == SquareStatus.SEA)
                referBattleshipGameJFrame.PrintMessage("No Battleship Chosen");
            else {
                Battleship battleship = squareRefference.getMyBattleship() ; 
                battleship.setCoordinate(coordinate);
                referBattleshipGameJFrame.removeBattleshipFromBoard(battleship);
                
            return;
            }
        }
        referBattleshipGameJFrame.currentCommand.setCoordinate(coordinate);
        Direction direction = referBattleshipGameJFrame.GetDirectionFromRadioButton();
        referBattleshipGameJFrame.currentCommand.setDirection(direction);
        referBattleshipGameJFrame.SetBattleshipInBoard(referBattleshipGameJFrame.currentCommand);
        SquareStatus status = squareRefference.getStatus();
        if (status == SquareStatus.BATTLESHIP) {
            this.setBackground(Color.red);
        }
    }
}
